package fitdroid.tabs;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import coderminus.maps.R;

public class RunSetupHost extends ActivityGroup {
	protected static final String TAG = "runsetup";

	private Button startbutton;
	private TableLayout contentlayout = null;
	private LocalActivityManager mActivityManager = null;
	private Intent origin = null;
	private Intent next = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Log.i(TAG, "intent_state set to " + Tracker.getInstance().getSem());
			if (Tracker.getInstance().getSem() > 0) {
				setBackView();
				
				Tracker.getInstance().down();
				Log.i(TAG, "intent_state set to (on back > 0)"
						+ Tracker.getInstance().getSem());
				return true;
			} else if (TabTracker.getInstance().getTabHost().getCurrentTab() > 0) {
				TabTracker
						.getInstance()
						.getTabHost()
						.setCurrentTab(
								TabTracker.getInstance().getTabHost()
										.getCurrentTab() - 1);
				return true;
			}
			return false;
		}
		return false;
	}

	private OnClickListener startbuttonListener = new OnClickListener() {
		public void onClick(View v) {
			Tracker.getInstance().up();
			Log.i(TAG, "intent_state set to (on start1)"
					+ Tracker.getInstance().getSem());

			setMapView();

			Log.i(TAG, "intent_state set to (on start2)"
					+ Tracker.getInstance().getSem());
		}
	};

	private void setMapView(){
		contentlayout.removeAllViews();
		contentlayout.setVisibility(View.VISIBLE);

		contentlayout.addView(mActivityManager.startActivity("", next)
				.getDecorView());
	}
	
	private void setBackView(){
		contentlayout.removeAllViews();
		contentlayout.setVisibility(View.VISIBLE);

		contentlayout.addView(mActivityManager.startActivity("",
				this.getIntent()).getDecorView());
		contentlayout.removeAllViews();
		contentlayout.setVisibility(View.VISIBLE);

		contentlayout.addView(mActivityManager
				.startActivity("", origin).getDecorView());
	}
	
	private void init() {
		setContentView(R.layout.runsetup);

		startbutton = (Button) findViewById(R.id.startbutton);
		startbutton.setOnClickListener(startbuttonListener);

		contentlayout = (TableLayout) findViewById(R.id.widget30);
		mActivityManager = getLocalActivityManager();
		origin = new Intent(this, runsetup.class);
		next = new Intent(this, coderminus.maps.MapsActivity.class);
	}
	
    /**...........Context Menu........cg.*/
	public boolean onCreateOptionsMenu (Menu menu) {
		
	    menu.add(0, 0, Menu.NONE, "Refresh");
	    menu.add(0, 1, Menu.NONE, "Exit");
	    return true;
	}

	/**---------- Handles item selections ------------cg */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case 0:
	    System.out.println("Refresh");
	    return true;

	    case 1:

	        System.out.println("Exit");

	    return true;

	    }
	    return false;
	}

}
